package com.gpsnav.ui;
//import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.ContextMenu;
import com.gpsnav.bo.*;
import com.gpsnav.util.Calculation;
import net.rim.device.api.system.Bitmap;
 
public class WaypointListField extends ObjectListField {

    Bitmap [] arrows = new Bitmap[8];
    Bitmap found = null;
    Bitmap dnf = null;
    Bitmap empty = Bitmap.getBitmapResource("empty.png");

    public WaypointListField() {
        super();
   
        found = Bitmap.getBitmapResource("images/found.png"); 
        dnf = Bitmap.getBitmapResource("images/DNF.png"); 
        
        for (int i = 0; i < arrows.length;i++) {
           arrows[i] = Bitmap.getBitmapResource("images/small/smallArrow"+i+".png"); 
         }

    }

    public void setContextMenu(ContextMenu cm) {
        makeContextMenu(cm);
    }
    
    public void drawListRow(ListField listField, Graphics g, int index, int y, int width)    {
        Waypoint wp = (Waypoint) get(listField,index);
        
        float bearing = wp.getBearing();
        Bitmap b = empty;
        String distance = "";
        
        if (bearing >= 0) {
            int dirIndex = (int) Calculation.round(bearing/45f,0);
            if (dirIndex >= 8) 
                dirIndex = 0;
            b = arrows[dirIndex];
        }
            
        int w = 0;
        int pos = 0;

        //found
        if (wp.isFound()) {
            g.drawBitmap(0, y, found.getWidth(),found.getHeight(), found, 0, 0);
        } else {
            if (wp.getFieldLog() != null && wp.getFieldLog().getType().equals(GeocachingLog.DNF)) {
                g.drawBitmap(0, y, dnf.getWidth(),dnf.getHeight(), dnf, 0, 0);
            }
        }
        pos += found.getWidth()+3;

        //direction
//        g.drawBitmap(0, y, b.getWidth(),b.getHeight(), b, 0, 0);
        g.drawBitmap(pos, y, b.getWidth(),b.getHeight(), b, 0, 0);
        pos += b.getWidth()+3;
        w = (int)(width*.2);
        
        int diff = 0;
        Font f = getFont();
        int fh = f.getHeight();
        if (fh < getRowHeight()) {
            diff = (int)((getRowHeight()-fh)/2);
        }
        int yPos = y+diff;
     
        //distance
        if (wp.getDistance() >= 0) {
            distance = Calculation.formatDistance(wp.getDistance());
        }
        
        //name
        g.drawText(distance, pos, yPos, DrawStyle.ELLIPSIS | DrawStyle.TOP, w);
        pos+=w+3;
        w = (int)(width-pos);
        g.drawText(wp.getName(), pos, yPos, DrawStyle.ELLIPSIS, w);
    }

    protected void paintBackground(Graphics g) {
        if (Options.getNightMode()) {
            g.setBackgroundColor(Color.BLACK);
            g.setColor(Color.RED);
        } else {
            g.setBackgroundColor(Color.WHITE);
            g.setColor(Color.BLACK);
        }
        g.clear();
    }

}
